package com.sk.rpcAssistNetty.io.netty.server;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.util.concurrent.Callable;

/**
 * description：workergroup的NioEventLoop中真正干活的handler
 *
 * @author 孙文斌
 * @Date: 2020/11/11 16:18
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    static final EventExecutorGroup group = new DefaultEventExecutorGroup(2);
    //读取客户端发送过来的消息
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        long start = System.currentTimeMillis();
        System.out.println("当前handler线程的名称为："+Thread.currentThread().getName()+"开始时间为："+start);
        group.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                ByteBuf byteBuf = (ByteBuf)msg;
                byte[] bytes = new byte[byteBuf.readableBytes()];
                byteBuf.readBytes(bytes);
                String msgStr = new String(bytes,"UTF-8");
                Thread.sleep(5*1000);
                long time = (System.currentTimeMillis()-start)/1000;
                System.out.println("业务线程池【任务1】任务队列当前线程的名称为："+Thread.currentThread().getName()+"相差时长为（秒）："+time);
                ctx.writeAndFlush(Unpooled.copiedBuffer("i am server1", CharsetUtil.UTF_8));
                return null;
            }
        });
//        group.submit(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                ByteBuf byteBuf = (ByteBuf)msg;
//                byte[] bytes = new byte[byteBuf.readableBytes()];
//                byteBuf.readBytes(bytes);
//                String msgStr = new String(bytes,"UTF-8");
//                Thread.sleep(5*1000);
//                long time = (System.currentTimeMillis()-start)/1000;
//                System.out.println("业务线程池【任务2】任务队列当前线程的名称为："+Thread.currentThread().getName()+"相差时长为（秒）："+time);
//                ctx.writeAndFlush(Unpooled.copiedBuffer("i am server2", CharsetUtil.UTF_8));
//                return null;
//            }
//        });



//        ctx.channel().eventLoop().execute(new Runnable() {
//            @Override
//            public void run()  {
//                try {
//                    Thread.sleep(5*1000);
//                    long time = (System.currentTimeMillis()-start)/1000;
//                    System.out.println("【任务1】任务队列当前线程的名称为："+Thread.currentThread().getName()+"相差时长为（秒）："+time);
//                } catch (InterruptedException e) {
//                    System.out.println("捕获到线程异常");
//                }
//            }
//        });
//        ctx.channel().eventLoop().execute(new Runnable() {
//            @Override
//            public void run()  {
//                try {
//                    Thread.sleep(5*1000);
//                    long time = (System.currentTimeMillis()-start)/1000;
//                    System.out.println("【任务2】任务队列当前线程的名称为："+Thread.currentThread().getName()+"相差时长为（秒）："+time);
//                } catch (InterruptedException e) {
//                    System.out.println("捕获到线程异常");
//                }
//            }
//        });
        
    }
    //读取客户端发送过来的消息
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("【完成读取】处理当前事件的eventLoop是："+ctx.pipeline().channel().eventLoop().hashCode());
    }
    //处理异常的方法
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("打印异常信息");
        cause.printStackTrace();
        ctx.close();
    }
}